package com.red.wood.module.eval.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.module.eval.dao.TaskFlowDao;

@Repository
public class TaskFlowDaoImpl implements TaskFlowDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private CommonDao dao;
	
	public void add(Map<String, Object> paramMap){
		StringBuilder sqlSb = new StringBuilder();
		sqlSb.append("insert into t_eval_task_flow (");
		dao.insertTableData(paramMap, sqlSb);
	}
	
	public List<Map<String, Object>> getBySingleTaskId(String singleTaskId){
		if(StringUtils.isNoneBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId,userName as userName, handleTime as handleTime,statue as statue, reason as reason from t_eval_task_flow where singleTaskId=" + singleTaskId + " order by handleTime desc";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(list != null && !list.isEmpty()){
				for(Map<String, Object> map : list){
					if(map.get("handleTime") != null){
						Date lastSubmitTime = (Date)map.get("handleTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						map.put("handleTime", formatter.format(lastSubmitTime));
					}
				}
			}
			return list;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getBySingleTaskIdAndStatue(String singleTaskId, String statue) {
		if(StringUtils.isNoneBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId,userName as userName, handleTime as handleTime,statue as statue, reason as reason from t_eval_task_flow "
					+ "where singleTaskId=" + singleTaskId 
					+ " and statue=" + "'" +  statue + "'"
					+ " order by handleTime desc";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(list != null && !list.isEmpty()){
				for(Map<String, Object> map : list){
					if(map.get("handleTime") != null){
						Date lastSubmitTime = (Date)map.get("handleTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						map.put("handleTime", formatter.format(lastSubmitTime));
					}
				}
			}
			return list;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getHistoryWorkOrderByFormNumber(String formNumber) {
		String sql = "select form_number from t_eval_pass_enterprise where form_number='"+formNumber+"'";
		return jdbcTemplate.queryForList(sql);
	}
	
	@Override
	public void insertHistoryWorkOrder(Map<String, Object> params) {
		StringBuilder sqlSb = new StringBuilder("insert into t_eval_pass_enterprise (");
		dao.insertTableData(params, sqlSb);
	}

	@Override
	public List<Map<String, Object>> getHistoryWorkOrderInfo(String singleTaskId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select t2.name           as enterprise_name,");
		sql.append("       t2.number         as form_number,");
		sql.append("       t1.applyBusiness  as apply_business,");
		sql.append("       t1.statue         as form_statue,");
		sql.append("       t2.address        as registered_address,");
		sql.append("       t2.productionUnit as production_unit,");
		sql.append("       case when t1.applyBusiness = 'ISP' or t1.applyBusiness = 'IDC(不含互联网资源协作)' then 1 ");
		sql.append("            else (select count(1) from t_eval_applicationcdn_node node where t1.id = node.singleTaskId) end as house_node_num,");
		sql.append("       (select count(2) from t_eval_application_coverinfo t where t.singleTaskId = t1.id) as link_num,");
		sql.append("       2 as form_type,");
		sql.append("       t1.id as singleTaskId");
		sql.append("  from t_eval_singletask t1");
		sql.append(" inner join t_eval_application_cdn t2");
		sql.append("    on t1.id = t2.singleTaskId");
		sql.append(" where t1.id = "+singleTaskId);
		return jdbcTemplate.queryForList(sql.toString());
	}

	@Override
	public void updateHistoryWorkOrder(Map<String, Object> params,String queryConf) {
		StringBuilder sql = new StringBuilder("update t_eval_pass_enterprise set ");
		dao.updateTableData(params, sql, queryConf);
	}
}
